const moment = require('moment');
const bcrypt = require('bcryptjs')
const db = require('../../config/mysql')
const { DataTypes } = require('sequelize')
const { dateFormat } = require('../../utils')

module.exports = db.define('user', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  name: {
    type: DataTypes.STRING(50),
    allowNull: false
  },
  sex: {
    type: DataTypes.STRING,
  },
  idCard: {
    type: DataTypes.STRING,
  },
  password: {
    type: DataTypes.STRING,
    set(val) {
      // 加密
      const salt = bcrypt.genSaltSync(10);
      // 生成加密密码
      const psw = bcrypt.hashSync(val, salt);
      this.setDataValue("password", psw);
    },
    allowNull: false
  },
  createTime: {
    type: DataTypes.STRING,
    allowNull: false,
    // get() {
    //   const time = this.getDataValue('createTime');
    //   return dateFormat(time, 'yyyy-MM-dd hh:mm:ss');
    // }
  }
}, {
  timestamps: false,
  tableName: 'user',
  freezeTableName: true
})
